home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / Tutorials / Tut1 / frmCapture.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  7.6 KB  |  217 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmCapture 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Audio Capture Tutorial"
  6.    ClientHeight    =   1395
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4305
  10.    Icon            =   "frmCapture.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   1395
  15.    ScaleWidth      =   4305
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComDlg.CommonDialog cdlSave 
  18.       Left            =   4140
  19.       Top             =   0
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.       CancelError     =   -1  'True
  24.    End
  25.    Begin VB.CommandButton cmdSave 
  26.       Caption         =   "Sa&ve"
  27.       Enabled         =   0   'False
  28.       Height          =   375
  29.       Left            =   3038
  30.       TabIndex        =   2
  31.       Top             =   840
  32.       Width           =   975
  33.    End
  34.    Begin VB.CommandButton cmdStop 
  35.       Caption         =   "&Stop"
  36.       Enabled         =   0   'False
  37.       Height          =   375
  38.       Left            =   1898
  39.       TabIndex        =   1
  40.       Top             =   840
  41.       Width           =   975
  42.    End
  43.    Begin VB.CommandButton cmdStart 
  44.       Caption         =   "&Record"
  45.       Height          =   375
  46.       Left            =   638
  47.       TabIndex        =   0
  48.       Top             =   840
  49.       Width           =   975
  50.    End
  51.    Begin VB.Label lbl 
  52.       BackStyle       =   0  'Transparent
  53.       Caption         =   "Audio Capture Tutorial"
  54.       Height          =   255
  55.       Index           =   0
  56.       Left            =   660
  57.       TabIndex        =   4
  58.       Top             =   120
  59.       Width           =   2655
  60.    End
  61.    Begin VB.Label lbl 
  62.       BackStyle       =   0  'Transparent
  63.       Caption         =   "Copyright (C) 1999-2001 Microsoft Corporation, All Rights Reserved."
  64.       Height          =   435
  65.       Index           =   2
  66.       Left            =   660
  67.       TabIndex        =   3
  68.       Top             =   360
  69.       Width           =   3510
  70.    End
  71.    Begin VB.Image Image1 
  72.       Height          =   480
  73.       Left            =   120
  74.       Picture         =   "frmCapture.frx":0442
  75.       Top             =   120
  76.       Width           =   480
  77.    End
  78. Attribute VB_Name = "frmCapture"
  79. Attribute VB_GlobalNameSpace = False
  80. Attribute VB_Creatable = False
  81. Attribute VB_PredeclaredId = True
  82. Attribute VB_Exposed = False
  83. Option Explicit
  84. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  85. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  86. '  File:       frmCapture.frm
  87. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  88. 'This tutorial will show basic functionality.  You will capture a buffer to memory,
  89. 'and then write it out to a file.
  90. 'Variable declarations for our app
  91. Private dx As New DirectX8
  92. Private dsc As DirectSoundCapture8
  93. Private dscb As DirectSoundCaptureBuffer8
  94. Private dscd As DSCBUFFERDESC
  95. Private capFormat As WAVEFORMATEX
  96. Private ds As DirectSound8
  97. Private Sub InitCapture()
  98.     Dim cCaps As DSCCAPS
  99.     On Local Error Resume Next
  100.     'We need to create a direct sound object before the capture object
  101.     If ds Is Nothing Then Set ds = dx.DirectSoundCreate(vbNullString)
  102.     If Err Then
  103.         MsgBox "Unable to create a DirectSound object", vbOKOnly Or vbCritical, "Cannot continue"
  104.         Cleanup
  105.         End
  106.     End If
  107.     'First we need to create our capture buffer on the default object
  108.     Set dsc = dx.DirectSoundCaptureCreate(vbNullString)
  109.     If Err Then
  110.         MsgBox "Unable to create a Capture object", vbOKOnly Or vbCritical, "Cannot continue"
  111.         Cleanup
  112.         End
  113.     End If
  114.     'Lets get the caps for our object
  115.     dsc.GetCaps cCaps
  116.     'Check for a capture format we will support in the sample
  117.     If cCaps.lFormats And WAVE_FORMAT_4M08 Then
  118.         capFormat = CreateWaveFormatEx(44100, 1, 8)
  119.     ElseIf cCaps.lFormats And WAVE_FORMAT_2M08 Then
  120.         capFormat = CreateWaveFormatEx(22050, 1, 8)
  121.     ElseIf cCaps.lFormats And WAVE_FORMAT_1M08 Then
  122.         capFormat = CreateWaveFormatEx(11025, 1, 8)
  123.     Else
  124.         MsgBox "Could not get the caps we need on this card.", vbOKOnly Or vbCritical, "Exiting."
  125.         Cleanup
  126.         End
  127.     End If
  128. End Sub
  129. Private Sub CreateCaptureBuffer()
  130.     dscd.fxFormat = capFormat
  131.     dscd.lBufferBytes = capFormat.lAvgBytesPerSec * 20
  132.     dscd.lFlags = DSCBCAPS_WAVEMAPPED
  133.     Set dscb = dsc.CreateCaptureBuffer(dscd)
  134. End Sub
  135. Private Sub Cleanup()
  136.     Set ds = Nothing
  137.     Set dscb = Nothing
  138.     Set dsc = Nothing
  139.     Set dx = Nothing
  140. End Sub
  141. Private Function CreateWaveFormatEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
  142.     'Create a WaveFormatEX structure using the vars we provide
  143.     With CreateWaveFormatEx
  144.         .nFormatTag = WAVE_FORMAT_PCM
  145.         .nChannels = Channels
  146.         .lSamplesPerSec = Hz
  147.         .nBitsPerSample = BITS
  148.         .nBlockAlign = Channels * BITS / 8
  149.         .lAvgBytesPerSec = .lSamplesPerSec * .nBlockAlign
  150.         .nSize = 0
  151.     End With
  152. End Function
  153. Private Sub cmdSave_Click()
  154.     On Local Error Resume Next
  155.     With cdlSave
  156.         'Set our initial properties
  157.         .FileName = vbNullString
  158.         .flags = cdlOFNHideReadOnly
  159.         .Filter = "Wave files(*.WAV)|*.wav"
  160.         .ShowOpen
  161.         If Err Then Exit Sub 'We clicked cancel
  162.         If .FileName = vbNullString Then Exit Sub 'No file
  163.         'Save the file to disk
  164.         GetSoundBufferFromCapture(dscb).SaveToFile .FileName
  165.     End With
  166. End Sub
  167. Private Sub cmdStart_Click()
  168.     'We want to record sound now.
  169.     'First we need to get rid of any sound we may have
  170.     Set dscb = Nothing
  171.     'Now get our capture buffer once more
  172.     CreateCaptureBuffer
  173.     'Now start recording
  174.     dscb.Start DSCBSTART_DEFAULT
  175.     'Disable/Enable our buttons accordingly
  176.     cmdStop.Enabled = True
  177.     cmdStart.Enabled = False
  178.     cmdSave.Enabled = False
  179. End Sub
  180. Private Sub cmdStop_Click()
  181.     Dim lbufferStatus As Long
  182.     'Stop the buffer
  183.     dscb.Stop
  184.     'Disable/Enable our buttons accordingly
  185.     cmdStop.Enabled = False
  186.     cmdStart.Enabled = True
  187.     cmdSave.Enabled = True
  188. End Sub
  189. Private Sub Form_Load()
  190.     'Lets init our capture device
  191.     InitCapture
  192. End Sub
  193. Private Sub Form_Unload(Cancel As Integer)
  194.     Cleanup
  195. End Sub
  196. Private Function GetSoundBufferFromCapture(ByVal oCaptureBuffer As DirectSoundCaptureBuffer8) As DirectSoundSecondaryBuffer8
  197.     Dim lbufferStatus As Long
  198.     Dim capCURS As DSCURSORS
  199.     Dim dsd As DSBUFFERDESC
  200.     Dim ByteBuffer() As Integer 'Our digital data from our capture buffer
  201.     'Are we still capturing? If so, stop
  202.     oCaptureBuffer.Stop
  203.     'Get the capture info
  204.     oCaptureBuffer.GetCurrentPosition capCURS
  205.     dsd.lBufferBytes = capCURS.lWrite + 1
  206.     dsd.fxFormat = dscd.fxFormat
  207.     'If there is nothing to write, then exit
  208.     If capCURS.lWrite = 0 Then Exit Function
  209.     Set GetSoundBufferFromCapture = ds.CreateSoundBuffer(dsd)
  210.     'Set the size for our new Data
  211.     ReDim ByteBuffer(capCURS.lWrite)
  212.     'Read the data from our capture buffer
  213.     oCaptureBuffer.ReadBuffer 0, capCURS.lWrite, ByteBuffer(0), DSCBLOCK_DEFAULT
  214.     'Write the data to our sound buffer
  215.     GetSoundBufferFromCapture.WriteBuffer 0, capCURS.lWrite, ByteBuffer(0), DSBLOCK_DEFAULT
  216. End Function
  217.